/**
 * 简化的存储管理工具
 */

// 存储键名常量
const STORAGE_KEYS = {
  VOICE_HISTORY: 'voice_history',
  AI_CHAT_HISTORY: 'ai_chat_history',
  AI_USER_QUICK: 'ai_user_quick',
  USER_PROFILE: 'user_profile',
  APP_SETTINGS: 'app_settings'
}

// 简化的存储管理对象
const storageManager = {
  // 基础存储方法
  setItem(key, value) {
    try {
      uni.setStorageSync(key, value)
      return true
    } catch (error) {
      console.error('存储失败:', error)
      return false
    }
  },

  getItem(key, defaultValue = null) {
    try {
      const value = uni.getStorageSync(key)
      return value !== '' ? value : defaultValue
    } catch (error) {
      console.error('读取存储失败:', error)
      return defaultValue
    }
  },

  removeItem(key) {
    try {
      uni.removeStorageSync(key)
      return true
    } catch (error) {
      console.error('删除存储失败:', error)
      return false
    }
  },

  // 语音历史相关
  saveVoiceHistory(history) {
    return this.setItem(STORAGE_KEYS.VOICE_HISTORY, history)
  },

  getVoiceHistory() {
    return this.getItem(STORAGE_KEYS.VOICE_HISTORY, [])
  },

  addVoiceHistoryItem(item) {
    const history = this.getVoiceHistory()
    history.unshift(item)
    // 限制历史记录数量
    if (history.length > 50) {
      history.splice(50)
    }
    return this.saveVoiceHistory(history)
  },

  // 聊天历史相关
  saveChatHistory(messages) {
    return this.setItem(STORAGE_KEYS.AI_CHAT_HISTORY, messages)
  },

  getChatHistory() {
    return this.getItem(STORAGE_KEYS.AI_CHAT_HISTORY, [])
  },

  // 用户快捷指令相关
  saveUserQuick(quickList) {
    return this.setItem(STORAGE_KEYS.AI_USER_QUICK, quickList)
  },

  getUserQuick() {
    return this.getItem(STORAGE_KEYS.AI_USER_QUICK, [])
  },

  addUserQuickItem(item) {
    const quickList = this.getUserQuick()
    if (!quickList.includes(item)) {
      quickList.push(item)
      return this.saveUserQuick(quickList)
    }
    return false
  },

  removeUserQuickItem(item) {
    const quickList = this.getUserQuick()
    const index = quickList.indexOf(item)
    if (index > -1) {
      quickList.splice(index, 1)
      return this.saveUserQuick(quickList)
    }
    return false
  },

  // 用户资料相关
  saveUserProfile(profile) {
    return this.setItem(STORAGE_KEYS.USER_PROFILE, profile)
  },

  getUserProfile() {
    return this.getItem(STORAGE_KEYS.USER_PROFILE, {
      name: '用户',
      avatar: '/static/default-avatar.svg',
      emergencyPhone: ''
    })
  },

  // 应用设置相关
  saveAppSettings(settings) {
    return this.setItem(STORAGE_KEYS.APP_SETTINGS, settings)
  },

  getAppSettings() {
    return this.getItem(STORAGE_KEYS.APP_SETTINGS, {
      theme: 'light',
      fontSize: 'normal',
      voiceEnabled: true
    })
  },

  // 阅读历史
  addReadHistory(newsItem) {
    const history = this.getItem('read_history', [])
    const existingIndex = history.findIndex(item => item.id === newsItem.id)
    
    if (existingIndex > -1) {
      history.splice(existingIndex, 1)
    }
    
    history.unshift({
      ...newsItem,
      readTime: Date.now()
    })
    
    // 限制历史记录数量
    if (history.length > 100) {
      history.splice(100)
    }
    
    return this.setItem('read_history', history)
  },

  getLastNewsUpdate() {
    return this.getItem('last_news_update', 0)
  },

  setLastNewsUpdate() {
    return this.setItem('last_news_update', Date.now())
  },

  // 清理过期数据
  cleanExpiredData() {
    const now = Date.now()
    const expireTime = 7 * 24 * 60 * 60 * 1000 // 7天
    
    // 清理过期的阅读历史
    const readHistory = this.getItem('read_history', [])
    const validHistory = readHistory.filter(item => 
      now - item.readTime < expireTime
    )
    
    if (validHistory.length !== readHistory.length) {
      this.setItem('read_history', validHistory)
    }
  }
}

export { storageManager }
